<?php 
require_once 'ckLogin.php';
require_once 'init.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'config.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'dbConn.php';
$db = new dbConn();
//删除
if(!empty($_GET['act'])){
	$act = $_GET['act'];
	if($act == 'del'){
		$db->sql = "DELETE FROM `comment2` WHERE id = ".$_GET['id'];
		$db->doExecute();
	}
	elseif($act == "set"){
		$openid = trim($_GET["openid"]);
		$ip = trim($_GET["ip"]);
		$date = date("Y-m-d");
		$db->sql = "replace into ForbiddenIds(`OpenId`, `Date`) values('{$openid}','{$date}');";
		
		if($ip){
			$db->sql .= "replace into ForbiddenIps(`IP`, `Date`) values('{$ip}','{$date}');";
		}
		
		$res = $db->doExecute();
		$msg = $res ? "禁言设置成功" : "禁言设置失败";
		echo "<script>alert('{$msg}');</script>";
	}
	elseif($act == "relieve"){
		$openid = trim($_GET["openid"]);
		$ip = trim($_GET["ip"]);
		$db->sql = "DELETE FROM ForbiddenIds WHERE `OpenId`='{$openid}';";
		
		if($ip){
			$db->sql .= "DELETE FROM ForbiddenIps WHERE `IP`='{$ip}';";
		}
		
		$res = $db->doExecute();
		$msg = $res ? "解禁成功" : "解禁失败";
		echo "<script>alert('{$msg}');</script>";
	}
	
}
//keyword
if($_GET['keyword']){
	$keyword=$_GET['keyword'];
	$where = " AND (c2.contentid='".$keyword."' OR c2.`comment` LIKE '%".$keyword."%') ";
}else{
	$keyword='';
	$where = '';
}
//总条数
$db->sql="SELECT COUNT(c2.id) FROM comment2 c2 LEFT JOIN content c1 ON c1.id=c2.contentid WHERE 1=1 ".$where;
$count = $db->doResultOne();
//分页
if(empty($_GET['page'])){$page = 1;}else{$page=$_GET['page'];}
if(empty($_GET['pagesize'])){$pagesize = 50;}
else{$pagesize = $_GET['pagesize'];}
$pageInfo = page($count, $pagesize, $page);
$maxPage = $pageInfo['maxPage'];
$start = $pageInfo['start'];
$pages = $pageInfo['pages'];
//var_dump($pageInfo);
$db->sql="SELECT c1.title,c2.*,c1.publishDate,f.OpenId FROM comment2 c2 LEFT JOIN content c1 ON c1.id=c2.contentid LEFT JOIN ForbiddenIds f ON f.OpenId = c2.userid WHERE 1=1 ".$where." ORDER BY c1.publishDate DESC,c1.sort,c1.id,c2.id DESC LIMIT ".$start.",".$pagesize;
$comments = $db->doResultList2();

$url="?page=".$page."&keyword=".$keyword;
//var_dump($comments);
//分页
function page($total,$size,$page){
	//最大页数
	if($total%$size==0){
		$maxPage = $total/$size;
	}else{
		$maxPage = (int)($total/$size)+1;
	}
	//开始条数
	$start = ($page-1)*$size;
	//分页
	if($maxPage>=5){
		if($page<=2){
			$pages=array(1,2,3,4,5);
		}elseif($page>=($maxPage-1)){
			$pages=array($maxPage-4,$maxPage-3,$maxPage-2,$maxPage-1,$maxPage);
		}else{
			$pages=array($page-2,$page-1,$page,$page+1,$page+2);
		}
	}else{
		for($i=1;$i<=$maxPage;$i++){
			$pages[]=$i;
		}
	}
	if($page<=1){$previous=1;}else{$previous=$page-1;}
	if($page>=$maxPage) {$next = $maxPage;}else{ $next = $page+1;}
	return array('maxPage'=>$maxPage,'start'=>$start,'pages'=>$pages,'previous'=>$previous,'next'=>$next);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo APPLICATION;?></title>
<!--                       CSS                       -->
<!-- Reset Stylesheet -->
<link rel="stylesheet" href="resources/css/reset.css" type="text/css" media="screen" />
<!-- Main Stylesheet -->
<link rel="stylesheet" href="resources/css/main.css" type="text/css" media="screen" />
<!-- Invalid Stylesheet. This makes stuff look pretty. Remove it if you want the CSS completely valid -->
<link rel="stylesheet" href="resources/css/invalid.css" type="text/css" media="screen" />

<!-- jQuery -->
<script type="text/javascript" src="resources/scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(function($) {

	$(".comment-edit").click(function(){
		var id = $(this).parents("tr").children("td").eq(0).text();
		var comment = $(this).parents("tr").children("td").eq(1).text();
		var cateids = $(this).parents("tr").find(".cateids").val();
		//alert(cateids);
		var cateidsArr = cateids.split(",");
		$.each(cateidsArr,function(key,id){
			$(".cateupdate[value="+id+"]").attr("checked", true);
		});
		$("#comment").text(comment);
		$("#commentID").val(id);
		$("#message_click").click();
	});
	$(".deleteUser").click(function(){
		if(!confirm("是否将此评论删除?")){
			return false;
		}
	});
	//批量删除
	$(".piliangshanchu").click(function(){
		if(confirm("是否删除这些商品！")){
			var ids='';
			$("input[name=picSel]:checked").each(function() {
				ids+=$(this).val()+',';
				$(this).parents("tr").remove();
			});
			$.ajax({
				type: "GET",
				url: "youpinAjax.php",
				data: "ids="+ids+"&act=piliangDelComment2",
				dataType: "html",
				success: function(date){
					alert("删除成功");
					
				}
			}); 
			//alert(ids);
		}
	});
});
</script>
<!-- jQuery Configuration -->
<script type="text/javascript"
	src="resources/scripts/simpla.jquery.configuration.js"></script>
<!-- Facebox jQuery Plugin -->
<script type="text/javascript" src="resources/scripts/facebox.js"></script>
<!-- jQuery WYSIWYG Plugin -->
<script type="text/javascript" src="resources/scripts/jquery.wysiwyg.js"></script>

</head>

<body>
	<div id="body-wrapper">
		<!-- Wrapper for the radial gradient background -->
		<a href="#messages" rel="modal" id="message_click"></a>
		<div id="messages" style="display: none;">
			<!-- Messages are shown when a link with these attributes are clicked: href="#messages" rel="modal"  -->

			<p style="">
				<strong>简短描述输入及修改</strong>
			</p>
			<form action="<?php echo "?page=".$page;?>" method="post">
				<fieldset>
					分类：
					<?php foreach ($cates as $keyX=>$itemX):?>
					<input class="cateupdate" name="cate[]" type="checkbox" value="<?php echo $itemX['id']?>" /><?php echo $itemX['cate'];?>
					<?php endforeach;?>
					<hr />
					描述：
					<textarea class="textarea" id="comment" name="comment" style="width: 380px;height: 90px;"></textarea>
					<input id="commentID" name="commentID" type="hidden" value="" />
					<input class="button" type="submit" value="修改" /> 
					<a href="javascript:void(0);" onclick="$(this).parents('#facebox').hide();">关闭</a>
				</fieldset>
			</form>
		</div>
		<!-- End #messages -->
		<div id="main-content">
			<!-- Page Head -->
			<h2>欢迎使用<?php echo APPLICATION;?>管理后台</h2>
			<p id="page-intro">评论管理</p>

			<div class="clear"></div>
			<!-- End .clear -->

			<div class="content-box">
				<!-- Start Content Box -->

				<div class="content-box-header">

					<h3>评论管理</h3>

					<div class="clear"></div>

				</div>
				<!-- End .content-box-header -->

				<div class="content-box-content">

				<!-- Start Notifications -->
				
				<div class="notification success png_bg" style="display: none;">
					<a href="#" class="close"><img
						src="resources/images/icons/cross_grey_small.png"
						title="Close this notification" alt="close" /></a>
					<div>
						修改成功
					</div>
				</div>
				
				<div class="notification error png_bg" style="display: none;">
					<a href="#" class="close"><img
						src="resources/images/icons/cross_grey_small.png"
						title="Close this notification" alt="close" /></a>
					<div>
					修改失败
					</div>
				</div>
				
				<!-- End Notifications -->
				
					<div class="tab-content default-tab" id="tab1">
						<!-- This is the target div. id must match the href of this div's tab -->
						
						<table>
							<thead>
								<tr>
									<td colspan="9">
										<div class="bulk-actions align-left">
										<form action="#" method="get">
											<input type="text" value="<?php echo $_GET['keyword'];?>" name="keyword" />
											<button class="button sousuo" type="submit">搜索</button>
										</form>
										
										</div>
									<?php if($maxPage>0):?>
										<div class="pagination">
											<a href="?page=1&keyword=<?php echo $keyword;?>" title="First Page">&laquo; First</a>
											<a href="?page=<?php echo  $pageInfo['previous'];?>&keyword=<?php echo $keyword;?>" title="Previous Page">&laquo; Previous</a>
											<?php foreach ($pages as $item):?> 
											<a href="?page=<?php echo $item;?>&keyword=<?php echo $keyword;?>" class="number <?php if($item == $page) echo 'current';?>" title="<?php echo $item;?>"><?php echo $item;?></a>
											<?php endforeach;?>
											<a href="?page=<?php echo $pageInfo['next'];?>&keyword=<?php echo $keyword;?>" title="Next Page">Next &raquo;</a>
											<a href="?page=<?php echo $maxPage;?>&keyword=<?php echo $keyword;?>" title="Last Page">Last &raquo;</a>
											total : <?php echo $maxPage;?>
										</div> 
									<?php endif;?>
										<div class="clear"></div><hr />
										<button class="button piliangshanchu" >批量删除</button>
									</td>
								</tr>
							</thead>
							<thead>
								<tr>
									<th><input class="check-all" type="checkbox" value="-1"/></th>
									<th style="width: 8%;">商品编号</th>
									<th style="width: 18%;">商品名称</th>
									<th style="width: 8%;">发布日期</th>
									<th style="width: 20%;">评论</th>
									<th style="width: 26%;">评论者ID</th>
									<th style="width: 11%;">评论者IP</th>
									<th style="width: 7%;">操作</th>
								</tr>

							</thead>

							<tbody>
							<?php if(count($comments)>0):?>
							<?php foreach ($comments as $key=>$item):?>
								<tr>
									<td><input type="checkbox" name="picSel" value="<?php echo $item['id'];?>"/></td>
									<td><?php echo $item['contentid'];?></td>
									<td><?php echo $item['title'];?></td>
									<th><?php echo $item['publishDate'];?></th>
									<td><?php echo $item['comment'];?></td>
									<td><?php echo $item['userid'];?></td>
									<td><?php echo $item['userip'];?></td>
									<td>
										<?php if($item["OpenId"]):?>
										<a style="color: red;" href="?openid=<?php echo $item['userid'];?>&ip=<?php echo $item['userip'];?>&act=relieve">解禁</a>
										<?php else:?>
										<a href="?openid=<?php echo $item['userid'];?>&ip=<?php echo $item['userip'];?>&act=set">禁言</a>
										<?php endif;?>
										<a class="deleteUser" href="?id=<?php echo $item['id'];?>&act=del&page=<?php echo $page;?>">删除</a>
									</td>
								</tr>
							<?php endforeach;?>
							<?php else:?>
								<tr style="font-size: 50px;text-align: center; font-weight: bold;"><td colspan="5" style="text-align: center;">没有评论！！</td></tr>
							<?php endif;?>
							</tbody>
							<tfoot>
								<tr>
									<td colspan="9">
									<button class="button piliangshanchu" >批量删除</button>
									<hr /><div class="clear"></div>
										
									<?php if($maxPage>0):?>
										<div class="pagination">
											<a href="?page=1&keyword=<?php echo $keyword;?>" title="First Page">&laquo; First</a>
											<a href="?page=<?php echo  $pageInfo['previous'];?>&keyword=<?php echo $keyword;?>" title="Previous Page">&laquo; Previous</a>
											<?php foreach ($pages as $item):?> 
											<a href="?page=<?php echo $item;?>&keyword=<?php echo $keyword;?>" class="number <?php if($item == $page) echo 'current';?>" title="<?php echo $item;?>"><?php echo $item;?></a>
											<?php endforeach;?>
											<a href="?page=<?php echo $pageInfo['next'];?>&keyword=<?php echo $keyword;?>" title="Next Page">Next &raquo;</a>
											<a href="?page=<?php echo $maxPage;?>&keyword=<?php echo $keyword;?>" title="Last Page">Last &raquo;</a>
											total : <?php echo $maxPage;?>
										</div> 
									<?php endif;?>
									</td>
								</tr>
							</tfoot>
						</table>

					</div>
					<!-- End #tab2 -->

				</div>
				<!-- End .content-box-content -->

			</div>
			<!-- End .content-box -->

			<div class="clear"></div>

			<div id="footer">
				<small> <!-- Remove this notice or replace it with whatever you want -->
					&#169; Copyright 2009 Your Company | Powered by <a
					href="http://themeforest.net/item/simpla-admin-flexible-user-friendly-admin-skin/46073">Simpla
						Admin</a> | <a href="#">Top</a>
				</small>
			</div>
			<!-- End #footer -->

		</div>
		<!-- End #main-content -->

	</div>
</body>


<!-- Download From www.exet.tk-->
</html>
